﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Security.Cryptography;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace BASE
{
    public class Utils
    {
        public static int getIntQuery(string id)
        {
            int number;
            if (HttpContext.Current.Request.QueryString[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.QueryString[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.QueryString[id].ToString());
            }
            return 0;
        }
        public static String getStringQuery(string id)
        {

            if (HttpContext.Current.Request.QueryString[id] != null)
            {

                return HttpContext.Current.Request.QueryString[id].ToString();
            }
            return "";
        }
        public static String getStringFrom(string id)
        {

            if (HttpContext.Current.Request.Form[id] != null)
            {

                return HttpContext.Current.Request.Form[id].ToString();
            }
            return "";
        }
        public static int getIntFrom(string id)
        {
            int number;
            if (HttpContext.Current.Request.Form[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.Form[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.Form[id].ToString());
            }
            return 0;
        }
        public static String getStringSession(String txt)
        {
            if (HttpContext.Current.Session[txt]!=null)
            {
                return HttpContext.Current.Session[txt].ToString();
            }
            return "";
        }
        public static int getIntSession(String txt)
        {
            int number;
            if (HttpContext.Current.Session[txt] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Session[txt].ToString(), out number)))
                {
                    return 0;
                }
                return Int32.Parse(HttpContext.Current.Session[txt].ToString());
            }
            return 0;
        }

        public static string getMD5(string txt)
        {
            string source = txt;
            MD5 md5 = MD5.Create();
            string hash = GetMd5Hash(md5, source);
            return hash;
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        // Verify a hash against a string. 
        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static string checkDataType(List<WebControl> lst)
        {
            int number;
            float numbet01;
            foreach (WebControl ctl in lst)
            {
                ctl.BackColor = System.Drawing.Color.White;
                if (ctl is System.Web.UI.WebControls.TextBox)
                {
                    TextBox text = (TextBox)ctl;
                    if (text.Text == "")
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Lỗi không được để trống";
                    }
                    
                    ctl.BackColor = System.Drawing.Color.White;
                }
                if (ctl.Attributes["DataType"] == "integer")
                {
                    if ((!Int32.TryParse(((ITextControl)ctl).Text, out number)))
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "loi nhap so lieu";
                    }
                }
                if (ctl.Attributes["DataType"] == "notnull")
                {
                    if (((ITextControl)ctl).Text == "")
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Lỗi không được để trống";
                    }
                }
                if (ctl.Attributes["MaxLength"] != null)
                {
                    int cout = int.Parse(ctl.Attributes["MaxLength"]);
                    if (cout < int.Parse(((ITextControl)ctl).Text))
                    {
                        return "Bạn nhập quá "+cout+" ký tự";
                    }
                }

                if (ctl.Attributes["DataType"] == "float")
                {
                    if ((!float.TryParse(((ITextControl)ctl).Text, out numbet01)))
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "loi nhap so lieu";
                    }
                }
                if (ctl.Attributes["DataType"] == "date")
                {
                    string input = ((ITextControl)ctl).Text;
                    DateTime dateTime;
                    var formats = new[] { "dd/M/yyyy", "dd/MM/yyyy", "yyyy-MM-dd" };
                    if (!DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Lỗi nhập ngày tháng";
                    }
                }
                if (ctl.Attributes["DataType"] == "email")
                {
                    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Match match = regex.Match(((ITextControl)ctl).Text);
                    if (!match.Success)
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Mail không đúng";
                    }
                }
                if (ctl.Attributes["DataType"] == "anh")
                {
                    if (ctl is System.Web.UI.WebControls.FileUpload)
                    {
                        FileUpload file = (FileUpload)ctl;
                        if (file.FileBytes.Length == 0)
                        {
                            file.BackColor = System.Drawing.Color.Yellow;
                            file.Focus();
                            return "chưa chọn file";
                        }
                        string ext = System.IO.Path.GetExtension(file.FileName).TrimStart(".".ToCharArray()).ToLower();
                        if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp"))
                        {
                            file.Focus();
                            file.BackColor = System.Drawing.Color.Yellow;
                            return "lỗi sai định dạng ảnh";
                        }

                    }
                }
                if (ctl.Attributes["DataType"] == "file")
                {
                    if (ctl is System.Web.UI.WebControls.FileUpload)
                    {
                        FileUpload file = (FileUpload)ctl;
                        if (file.FileBytes.Length == 0)
                        {
                            file.BackColor = System.Drawing.Color.Yellow;
                            file.Focus();
                            return "chưa chọn file";
                        }
                        string ext = System.IO.Path.GetExtension(file.FileName).TrimStart(".".ToCharArray()).ToLower();
                        if ((ext == "exe") || (ext == "dll"))
                        {
                            file.Focus();
                            file.BackColor = System.Drawing.Color.Yellow;
                            return "lỗi sai định dạng ảnh";
                        }

                    }
                }
                if (ctl.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
                {
                    DropDownList drop = (DropDownList)ctl;
                    if (drop.SelectedValue == "0")
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Lỗi không được để trống";
                    }
                    ctl.BackColor = System.Drawing.Color.White;
                }
                if (ctl.GetType().ToString() == "System.Web.UI.WebControls.CheckBoxList")
                {
                    CheckBoxList drop = (CheckBoxList)ctl;
                    if (drop.SelectedValue == "")
                    {
                        ctl.Focus();
                        ctl.BackColor = System.Drawing.Color.Yellow;
                        return "Lỗi không được để trống";
                    }
                    ctl.BackColor = System.Drawing.Color.White;
                }

            }
            return "";
        }
    }
}
